package jee.boot.module.iexp.task.impl;

import jee.boot.common.basic.result.MessageCodeHelper;
import jee.boot.common.properties.ProjectConfig;
import jee.boot.common.remote.RemoteEndpoint;
import jee.boot.common.standard.AccountHandler;
import jee.boot.common.standard.AuthorityHandler;
import jee.boot.common.utils.JeeBootContext;
import jee.boot.common.utils.SpringContextHolder;
import jee.boot.entity.task.IexpTask;

public class BasicTaskRunnable {
    protected AuthorityHandler authorityHandler;
    protected AccountHandler accountHandler;
    protected RemoteEndpoint remoteEndpoint;
    protected ProjectConfig projectConfig;

    /**
     * 配置权限
     *
     * @param task
     */
    protected void configThreadPermission(IexpTask task) {
        // 清除历史的权限信息,如果存在
        //设置后台任务的引用URI
        JeeBootContext.set(JeeBootContext.REFERER_URI, task.getRefererUri());
        JeeBootContext.set(JeeBootContext.LANGUAGE, task.getLanguage());
        JeeBootContext.set(JeeBootContext.AUTHORIZED_USER, task.getCreateBy().getId());
        if (authorityHandler.getUserByAccount(task.getCreateBy().getId()) == null) {
            remoteEndpoint.executePost(projectConfig.getOpenOauthServer(), "/user/authorization", task.getCreateBy(), Object.class);
        }
    }
    public MessageCodeHelper getMessageCodeHelper() {
        return SpringContextHolder.getApplicationContext().getBean(MessageCodeHelper.class);
    }
}
